@model Netnr.Blog.Domain.Gist

@{
    ViewData["Title"] = Model.GistRemark;
}

<div class="container-fluid">
    <div class="cp-header">
        <div class="row mt-3">
            <div class="col-12 text-right">
                <div class="float-left">
                    <i class="fa fa-user fa-fw fa-2x text-muted mr-sm-2"></i>
                </div>
                <div class="float-left text-left">
                    <a href="/gist/user/@Model.Uid">@Model.Spare3</a>&nbsp;/
                    <a href="/gist/code/@Model.GistCode"><b>@Model.GistFilename</b></a>
                    <div>
                        <small class="text-muted">@Model.GistCreateTime.Value.ToString("yyyy-MM-dd HH:mm")</small>
                    </div>
                </div>

                @{
                    var hc = ViewContext.HttpContext;
                    bool IsLogin = hc.User.Identity.IsAuthenticated;
                    var uinfo = Netnr.Blog.Web.Apps.LoginService.Get(ViewContext.HttpContext);
                }
                @if (IsLogin && Model.Uid == uinfo.UserId)
                {
                    <a class="btn btn-outline-secondary btn-sm mr-sm-2" href="/gist/code/@Model.GistCode/edit">
                        <i class="fa fa-pencil"></i>&nbsp;Edit
                    </a>
                    <a class="btn btn-outline-danger btn-sm" href="/gist/code/@Model.GistCode/delete" onclick="return confirm('Are you positive you want to delete this Gist?')">
                        <i class="fa fa-trash-o"></i>&nbsp;Delete
                    </a>
                }
            </div>
        </div>
        <div class="row mb-2">
            <div class="col-md-7"></div>
            <div class="col-md-5 col-sm-12">
                <div class="input-group input-group-sm mt-3">
                    <div class="input-group-prepend">
                        <select class="btn btn-secondary" id="segistc">
                            <option value="Share">Share</option>
                        </select>
                    </div>
                    <input type="text" class="form-control" readonly="readonly" id="txtgistc">
                    <div class="input-group-prepend" id="copygistc">
                        <span class="btn btn-secondary fa fa-copy"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row" id="fileview">
        <div class="col-md-12 mt-4 mb-3">
            @Model.GistRemark
        </div>
        <div class="col-md-12">
            <div class="card gc-viewcode">
                <div class="card-header">
                    <i class="fa fa-fw fa-spinner fa-spin" id="faicon"></i>&nbsp;
                    <a href="#fileview">@Model.GistFilename</a>
                    <small class="ml-3 badge badge-warning">@Model.GistLanguage</small>
                    <div class="btn-group float-right">
                        <a href="/gist/raw/@Model.GistCode" class="btn btn-outline-secondary">Raw</a>
                        <a href="/gist/raw/@Model.GistCode/download" class="btn btn-outline-secondary">Download</a>
                    </div>
                </div>
                @if (Model.GistLanguage == "markdown")
                {
                    @Html.Raw(Netnr.SharedApp.QuoteTo.Html("netnrmd.css,netnrmd.js,monaco-editor"))

                    <div class="markdown-body px-2 py-2">@Model.GistContent</div>
                    <script>
                        var mb = $('div.markdown-body');
                        mb.html(netnrmd.render(mb.html()));

                        $('#faicon').removeClass('fa-spinner fa-spin').addClass('fa-file-code-o');
                    </script>
                }
                else
                {
                    <pre class="card-body monaco-editor vcln markdown-body" style="height:@((19*Model.GistRow+15).ToString()+"px");overflow-y:hidden;padding-left:65px;" data-lang="@Model.GistLanguage" data-theme="@Model.GistTheme">@Model.GistContent</pre>
                    @Html.Raw(Netnr.SharedApp.QuoteTo.Html("monaco-editor"))

                    <script>
                        var editor = null;
                        require(['vs/editor/editor.main'], function () {
                            var gc = $('pre.vcln');
                            var cv = htmlDecodeByRegExp(gc.html());
                            gc.html('');
                            editor = monaco.editor.create(gc[0], {
                                value: cv,
                                language: gc.attr('data-lang'),
                                automaticLayout: true,
                                roundedSelection: false,
                                scrollBeyondLastLine: false,
                                contextmenu: false,
                                scrollbar: {
                                    verticalScrollbarSize: 0,
                                    horizontalScrollbarSize: 9,
                                    alwaysConsumeMouseWheel: false
                                },
                                readOnly: true,
                                theme: gc.attr('data-theme'),
                                minimap: {
                                    enabled: false
                                }
                            });
                            gc.css('padding-left', 0);
                            $('#faicon').removeClass('fa-spinner fa-spin').addClass('fa-file-code-o');
                        });
                    </script>
                }
            </div>
        </div>

        <!--同步镜像-->
        <div class="col-md-12 mt-3">
            @if (!string.IsNullOrWhiteSpace(Model.Spare1))
            {
                <div title="GitHub 镜像">
                    <i class="fa fa-fw fa-github"></i>
                    <a target="_blank" href="https://gist.github.com/netnr/@Model.Spare1">https://gist.github.com/netnr/@Model.Spare1</a>
                </div>
            }
            @if (!string.IsNullOrWhiteSpace(Model.Spare2))
            {
                <div title="Gitee 镜像">
                    <i class="fa fa-fw fa-git"></i>
                    <a target="_blank" href="https://gitee.com/netnr/codes/@Model.Spare2">https://gitee.com/netnr/codes/@Model.Spare2</a>
                </div>
            }
        </div>
    </div>
</div>

<script>
    var sciarr = [
        location.href
    ];
    $('#segistc').change(function () {
        $('#txtgistc').val(sciarr[this.selectedIndex]);
    }).parent().next().val(sciarr[0]);
    $('#txtgistc').on('focus click', function () {
        this.focus();
        this.select();
    }).val(sciarr[0]);

    $('#copygistc').click(function () {
        $('#txtgistc')[0].focus();
        $('#txtgistc')[0].select();
    });

</script>